<template>
  <div class='my-container'>
    <!-- 未登录 -->
    <div class="header not-login" v-if="!user">
      <div class="login-btn" @click="$router.push('/login')">
        <img class="mobile-img" src="~@/assets/mobile.png" alt="" width="90px" height="90px">
        <span class="text">登录/注册</span>
      </div>
    </div>
    <!-- 已登录 -->
    <div class="header user-info" v-if="user">
      <div class="base-info">
        <div class="left">
          <van-image round class="avatar" fit="cover" :src=userInfo.photo>
            <template v-slot:error>加载失败</template>
          </van-image>
          <span class="name">{{userInfo.name}}</span>
        </div>
        <div class="right">
          <van-button type="default" size="mini" round>编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span class="count">{{userInfo.gender}}</span>
          <span class="text">头条</span>
        </div>
        <div class="data-item">
          <span class="count">10</span>
          <span class="text">关注</span>
        </div>
        <div class="data-item">
          <span class="count">110</span>
          <span class="text">粉丝</span>
        </div>
        <div class="data-item">
          <span class="count">{{userInfo.gender}}</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>
    <!-- 导航 -->
    <van-grid clickable :column-num="2" class="grid-nav">
      <van-grid-item class="grid-item">
        <van-icon slot="icon" name="star-o" />
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-item">
        <van-icon slot="icon" name="clock-o" class="lishi" />
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>
    <van-cell title="消息通知" is-link />
    <van-cell class="mb-9" title="小明同学" is-link />
    <van-cell v-if="user" class="logout-cell" title-class="logout" title="退出登录" @click="exitLogout" />
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user'
export default {
  name: 'myIndex',
  data () {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  components: {},
  methods: {
    // 退出登录
    exitLogout () {
      this.$dialog
        .confirm({
          title: '确认退出吗'
        })
        .then(() => {
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
        })
    },
    // 获取用户信息
    async getUser () {
      try {
        if (this.user) {
          const res = await getUserInfo(this.user.token)
          this.userInfo = res.data.data
          // console.log(this.userInfo)
        }
      } catch (err) {
        this.$toast('获取用户失败,请稍后重试')
      }
    }
  },
  created () {
    this.getUser()
  },
  mounted () {}
}
</script>

<style lang='less' scoped>
.my-container {
  .header {
    height: 361px;
    background-color: rgb(50, 150, 250);
    background-size: cover;
  }
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .mobile-img {
        margin-bottom: 15px;
      }
      .text {
        font-size: 36px;
        color: #fff;
      }
    }
  }

  .user-info {
    .base-info {
      height: 231px;
      padding: 76px 32px 23px;
      box-sizing: border-box; //不要撑开
      display: flex;
      justify-content: space-between;
      align-items: center;

      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          border: 4px solid #fff;
          margin-right: 23px;
        }
        .name {
          font-size: 30px;
          color: #fff;
        }
      }
    }
    .data-stats {
      display: flex;
      .data-item {
        height: 130px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
  .grid-nav {
    .grid-item {
      height: 141px;
      .van-icon {
        font-size: 45px;
        color: #eb5253;
      }
      .text {
        font-size: 28px;
      }
    }
  }
  .logout {
    color: red;
    margin-left: 280px;
  }
}
</style>
